package com.wlr.study.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 判断是否是同样的树
 *
 * @author lrwang
 * @version 1.0
 * @date 2022/4/5 23:44
 */
public class LeetCode100 {
    List<Integer> p1pre = new ArrayList<>();
    List<Integer> p1inorder = new ArrayList<>();
    List<Integer> p2pre = new ArrayList<>();
    List<Integer> p2inorder = new ArrayList<>();

    public boolean isSameTree(TreeNode p, TreeNode q) {
//        if (p == null && q == null) {
//            return true;
//        }
//        if (p == null || q == null) {
//            return false;
//        }
//        p1pre = preOrder(p, p1pre);
//        p2pre = preOrder(q, p2pre);
//        p1inorder = inOrder(p, p1inorder);
//        p2inorder = inOrder(q, p2inorder);
//        System.out.println(p1pre + "\n" + p2pre + "\n" + p1inorder + "\n" + p2inorder);
//        return p1pre.equals(p2pre) && p1inorder.equals(p2inorder);
        if (p == null && q == null) {
            return true;
        } else if (p == null || q == null) {
            return false;
        } else if (p.val != q.val) {
            return false;
        } else {
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        }
    }

    public List<Integer> preOrder(TreeNode root, List<Integer> result) {
        if (root == null) {
            result.add(null);
            return null;
        }
        result.add(root.val);
        preOrder(root.left, result);
        preOrder(root.right, result);
        return result;
    }

    public List<Integer> inOrder(TreeNode root, List<Integer> result) {
        if (root == null) {
            result.add(null);
            return null;
        }
        preOrder(root.left, result);
        result.add(root.val);
        preOrder(root.right, result);
        return result;
    }

    public static void main(String[] args) {
        LeetCode100 leetcode100 = new LeetCode100();
        TreeNode p = new TreeNode(1);
        TreeNode p1 = new TreeNode(1);
        p.left = p1;
        TreeNode q = new TreeNode(1);
        TreeNode q1 = new TreeNode(1);
        q.right = q1;
        leetcode100.isSameTree(p, q);
    }
}
